﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AAPCVM.Interfaces;
using System.Media;
namespace AAPCVM
{
    public partial class ctrlCandidateInfo : UserControl
    {
        private IController _controller;
        public string CandidateId
        {
            get
            {
                return textBox1.Text;
            }
        }


        public ctrlCandidateInfo()
        {
            InitializeComponent();
        }

        public void InitializeControl(IController controller)
        {
            _controller = controller;
        }

        public void Reset()
        {
            textBox1.Text = string.Empty;
            picBoxCandidate.Image = null;
            textBox1.Enabled = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 2)
            {
                if (_controller.IsIdDuplicate(textBox1.Text))
                {
                    textBox1.TextChanged -= this.textBox1_TextChanged;
                    textBox1.Text = string.Empty;
                    textBox1.TextChanged += this.textBox1_TextChanged;
                    SystemSounds.Exclamation.Play();
                }
                else
                {
                    _controller.AddCandidate(textBox1.Text);
                    SendKeys.Send("{TAB}");
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            picBoxCandidate.Image = _controller.GetCandidateImage(textBox1.Text);
        }
    }
}
